import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
import 'package:loxen/api/http_server/user_info.dart';
import 'package:loxen/mode/mine/mine_information_mode.dart';
import 'package:loxen/mode/order_detail_mode.dart';
import 'package:loxen/mode/order_list_mode.dart';
import 'package:loxen/mode/staff_info_mode.dart';
import 'package:loxen/viewmode/base_view_model.dart';
import 'package:loxen/api/api.dart';
import 'package:loxen/mode/base_mode.dart';

import 'package:rxdart/rxdart.dart';

class MinePageViewModel extends BaseViewModel with UserInfoMixin {
  MineInformation _data;
  StaffInfo staffInfo;
  bool staff = false;
  List<OrderDetailMode> listOrder;

  MineInformation get data => _data;

  void changeStaff() {
    staff = !staff;
    loadData();
  }

  void changeEntity() {
    notifyListeners();
  }

  Stream<StaffInfo> loadStaffInfo() {
    return ApiService.staffInfo()
        .flatMap((value) => compute(decodeStaffInfo, value.data).asStream())
        .flatMap((value) => Stream.value(value?.data));
  }

  Stream<StaffOrderList> loadSchedule() {
    return ApiService.schedule('0')
        .flatMap((value) => compute(decodeOrderList, value.data).asStream())
        .flatMap((value) => Stream.value(value?.data));
  }

  //加载内容数据
  void loadData() {
    if (staff) {
      Rx.zip2<StaffInfo, StaffOrderList, bool>(loadStaffInfo(), loadSchedule(), (a, b) {
        if (b != null) {
          listOrder = b.results;
        }
        if (a != null) {
          staffInfo = a;
        }
        if (a != null && b != null) {
          return true;
        }
        return false;
      }).doOnListen(() {
        mState = BaseState.LOADING;
        notifyListeners();
      }).listen((event) {
        if (event) {
          mState = BaseState.CONTENT;
        } else {
          mState = BaseState.FAIL;
        }
      }, onError: (error, stack) {
        mState = BaseState.FAIL;
      }, onDone: () => notifyListeners());
    } else {
      userInfo().doOnListen(() {
        mState = BaseState.LOADING;
        notifyListeners();
      }).listen((event) {
        if (event != null) {
          _data = event;
          mState = BaseState.CONTENT;
        } else {
          mState = BaseState.EMPTY;
        }
      }, onError: (error) {
        mState = BaseState.CONTENT;
        print(error);
      }, onDone: () => notifyListeners());
    }
  }

  void modifyBackground(FormData background,
      {Function() success, Function(Exception error) onError}) {
    ApiService.modifyBackground(background).listen((event) {
      if (event.statusCode == 200) {
        this._data.user.background = event.data['data']['background'];
        notifyListeners();
        if (success != null) success.call();
      } else {
        if (onError != null) onError.call(Exception(event.data));
      }
    }, onError: (error) {
      if (onError != null) onError.call(error);
      if (error.portrait != null && error.background[0] != '') {
        print(error.portrait[0]);
      }
    });
  }

  static BaseMode<StaffInfo> decodeStaffInfo(dynamic json) {
    return BaseMode<StaffInfo>(json);
  }

  static BaseMode<StaffOrderList> decodeOrderList(dynamic json) {
    return BaseMode<StaffOrderList>(json);
  }

  static MineInformation decodeMineInformationResults(dynamic json) {
    return MineInformation.fromJson(json);
  }

  static BaseMode<MineInformation> decodeMineInfoData(dynamic json) {
    return BaseMode(json);
  }
}
